﻿package com.yy.ui.controls {
    import flash.events.*;
    import flash.utils.*;
    import flash.display.*;
    import com.fminutes.vinciUI.events.*;
    import com.fminutes.vinciUI.containers.*;
    import com.fminutes.vinciUI.uiconst.*;

    public class ListBase extends Box {

        private var _itemClass:Class;
        private var _itemWidth:Number;
        private var _itemHeight:Number;
        private var _itemHDistance:Number;
        private var _itemVDistance:Number;
        private var _direction:String;
        private var _cItemHDistance:Number;
        private var _cItemVDistance:Number;
        private var _cHCount:int = 0;
        private var _cVCount:int = 0;
        private var _page1:Sprite;
        private var _page2:Sprite;
        private var _page1Index:int = 0;
        private var _page2Index:int = 1;
        private var _cPage:Sprite;
        private var _sizeChange:Boolean = false;
        private var _source:Array;
        private var _selectedIndex:int = -1;
        private var _rolling:Boolean = false;
        private var _preEnterFrameTime:int = 0;
        private var _rollSpeedX:Number = 0;
        private var _rollSpeedY:Number = 0;
        private var _targetX:Number = 0;
        private var _targetY:Number = 0;

        public function ListBase(_arg1:Class, _arg2:Number, _arg3:Number, _arg4:Number=0, _arg5:Number=0, _arg6:String="horizontal"){
            this._itemClass = _arg1;
            this._itemWidth = _arg2;
            this._itemHeight = _arg3;
            this._itemVDistance = _arg5;
            this._itemHDistance = _arg4;
            this._direction = _arg6;
            this._page1 = new Sprite();
            this._page2 = new Sprite();
            addChild(this._page1);
            addChild(this._page2);
            this._cPage = this._page1;
            this._sizeChange = true;
            if (this.stage){
                this.stage.invalidate();
            };
            addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
        }
        private function operationCount(_arg1:Number, _arg2:Number, _arg3:Number, _arg4:Number, _arg5:Number, _arg6:Number):void{
            if ((((_arg1 < _arg3)) || ((_arg2 < _arg4)))){
                this._cHCount = 0;
                this._cVCount = 0;
            } else {
                this._cHCount = this.getItemCount(_arg1, _arg3, _arg5);
                this._cVCount = this.getItemCount(_arg2, _arg4, _arg6);
            };
            if (this._cHCount > 1){
                this._cItemHDistance = ((_arg1 - (_arg3 * this._cHCount)) / (this._cHCount - 1));
            } else {
                this._cItemHDistance = 0;
            };
            if (this._cVCount > 1){
                this._cItemVDistance = ((_arg2 - (_arg4 * this._cVCount)) / (this._cVCount - 1));
            } else {
                this._cItemVDistance = 0;
            };
        }
        protected function createChild(_arg1:DisplayObjectContainer, _arg2:Class, _arg3:int):void{
            var _local5:Sprite;
            var _local6:Sprite;
            var _local4:int = _arg3;
            while (_arg1.numChildren < _local4) {
                _local5 = new (_arg2)();
                _local5.addEventListener(MouseEvent.CLICK, this.itemClickHandler);
                _arg1.addChild(_local5);
            };
            while (_arg1.numChildren > _local4) {
                _local6 = (_arg1.getChildAt(0) as Sprite);
                if (_local6.hasEventListener(MouseEvent.CLICK)){
                    _local6.removeEventListener(MouseEvent.CLICK, this.itemClickHandler);
                };
                _arg1.removeChild(_local6);
            };
        }
        public function getListItem():Array{
            var _local2:int;
            var _local1:Array = [];
            _local2 = 0;
            while (_local2 < this._page1.numChildren) {
                _local1.push(this._page1.getChildAt(_local2));
                _local2++;
            };
            _local2 = 0;
            while (_local2 < this._page2.numChildren) {
                _local1.push(this._page2.getChildAt(_local2));
                _local2++;
            };
            return (_local1);
        }
        public function getListItemByDataIndex(_arg1:int):ListItem{
            var _local4:ListItem;
            var _local6:ListItem;
            var _local2:Array = this.getListItem();
            var _local3:int = _local2.length;
            var _local5:int;
            while (_local5 < _local3) {
                _local6 = (_local2[_local5] as ListItem);
                if (((_local6) && ((_local6.index == _arg1)))){
                    _local4 = _local6;
                    break;
                };
                _local5++;
            };
            return (_local4);
        }
        protected function updateItemPosition(_arg1:DisplayObjectContainer, _arg2:int, _arg3:int, _arg4:Number, _arg5:Number, _arg6:Number, _arg7:Number):void{
            var _local8:int;
            var _local9:int;
            var _local10:int;
            while (_local10 < _arg1.numChildren) {
                _arg1.getChildAt(_local10).x = (_local8 * (_arg4 + _arg6));
                _arg1.getChildAt(_local10).y = (_local9 * (_arg5 + _arg7));
                _local8++;
                if (_local8 >= _arg2){
                    _local8 = 0;
                    _local9++;
                };
                _local10++;
            };
        }
        override public function set cX(_arg1:Number):void{
            super.cX = _arg1;
            if (this._direction != Direction.HORIZONTAL){
                return;
            };
            var _local2:int = this._page1Index;
            var _local3:int = this._page2Index;
            var _local4:Number = (this._cHCount * (this._itemWidth + this._cItemHDistance));
            var _local5:int = ((_arg1 / _local4) + 0.0001);
            var _local6:Boolean;
            if ((_local5 % 2) == 0){
                if (((!((this._page1Index == _local5))) || (!((this._cPage == this._page1))))){
                    this._cPage = this._page1;
                    this._page1Index = _local5;
                    this._page2Index = (_local5 + 1);
                    _local6 = true;
                };
            } else {
                if (((!((this._page2Index == _local5))) || (!((this._cPage == this._page2))))){
                    this._cPage = this._page2;
                    this._page1Index = (_local5 + 1);
                    this._page2Index = _local5;
                    _local6 = true;
                };
            };
            if (_local6){
                this._cPage.x = (_local4 * _local5);
                this.setPagePosition();
                if (this._page1Index != _local2){
                    this.setData(this._page1, this._page1Index);
                };
                if (this._page2Index != _local3){
                    this.setData(this._page2, this._page2Index);
                };
            };
        }
        override public function set cY(_arg1:Number):void{
            super.cY = _arg1;
            if (this._direction != Direction.VERTICAL){
                return;
            };
            var _local2:int = this._page1Index;
            var _local3:int = this._page2Index;
            var _local4:Number = (this._cVCount * (this._itemHeight + this._cItemVDistance));
            var _local5:int = ((_arg1 / _local4) + 0.0001);
            var _local6:Boolean;
            if ((_local5 % 2) == 0){
                if (((!((this._page1Index == _local5))) || (!((this._cPage == this._page1))))){
                    this._cPage = this._page1;
                    this._page1Index = _local5;
                    this._page2Index = (_local5 + 1);
                    _local6 = true;
                };
            } else {
                if (((!((this._page2Index == _local5))) || (!((this._cPage == this._page2))))){
                    this._cPage = this._page2;
                    this._page1Index = (_local5 + 1);
                    this._page2Index = _local5;
                    _local6 = true;
                };
            };
            if (_local6){
                this._cPage.y = (_local4 * _local5);
                this.setPagePosition();
                if (this._page1Index != _local2){
                    this.setData(this._page1, this._page1Index);
                };
                if (this._page2Index != _local3){
                    this.setData(this._page2, this._page2Index);
                };
            };
        }
        public function set source(_arg1:Array):void{
            this._source = _arg1;
            this.setData(this._page1, this._page1Index);
            this.setData(this._page2, this._page2Index);
        }
        public function get source():Array{
            return (this._source);
        }
        private function setData(_arg1:DisplayObjectContainer, _arg2:int):void{
            var _local3:int;
            var _local4:int;
            var _local5:Object;
            var _local6:IListItem;
            if (this.source == null){
                return;
            };
            _local4 = (_arg2 * (this._cHCount * this._cVCount));
            _local3 = 0;
            while (_local3 < _arg1.numChildren) {
                _local5 = this.source[(_local4 + _local3)];
                _local6 = (_arg1.getChildAt(_local3) as IListItem);
                if (_local5){
                    if (_local6.data != _local5){
                        _local6.data = _local5;
                    };
                    (_local6 as DisplayObject).visible = true;
                } else {
                    (_local6 as DisplayObject).visible = false;
                };
                if ((_local4 + _local3) == this.selectedIndex){
                    _local6.selected = true;
                } else {
                    if (_local6.selected == true){
                        _local6.selected = false;
                    };
                };
                _local6.index = (_local4 + _local3);
                _local3++;
            };
        }
        private function setPagePosition():void{
            var _local1:Sprite;
            var _local2:Sprite;
            var _local3:Boolean;
            if (this._cPage == this._page1){
                _local1 = this._page1;
                _local2 = this._page2;
                if (this._page2Index > this._page1Index){
                    _local3 = true;
                } else {
                    _local3 = false;
                };
            } else {
                _local1 = this._page2;
                _local2 = this._page1;
                if (this._page1Index > this._page2Index){
                    _local3 = true;
                } else {
                    _local3 = false;
                };
            };
            if (this._direction == Direction.HORIZONTAL){
                if (_local3){
                    _local2.x = (_local1.x + ((this._itemWidth + this._cItemHDistance) * this._cHCount));
                } else {
                    _local2.x = (_local1.x - ((this._itemWidth + this._cItemHDistance) * this._cHCount));
                };
                _local2.y = _local1.y;
            } else {
                if (_local3){
                    _local2.y = (_local1.y + ((this._itemHeight + this._cItemVDistance) * this._cVCount));
                } else {
                    _local2.y = (_local1.y - ((this._itemHeight + this._cItemVDistance) * this._cVCount));
                };
                _local2.x = _local1.x;
            };
        }
        override protected function updateDisplayList():void{
            super.updateDisplayList();
            if (this._sizeChange){
                this.operationCount(this.width, this.height, this._itemWidth, this._itemHeight, this._itemHDistance, this._itemVDistance);
                this.createChild(this._page1, this._itemClass, (this._cHCount * this._cVCount));
                this.createChild(this._page2, this._itemClass, (this._cHCount * this._cVCount));
                this.updateItemPosition(this._page1, this._cHCount, this._cVCount, this._itemWidth, this._itemHeight, this._cItemHDistance, this._cItemVDistance);
                this.updateItemPosition(this._page2, this._cHCount, this._cVCount, this._itemWidth, this._itemHeight, this._cItemHDistance, this._cItemVDistance);
                this._sizeChange = false;
                this.setPagePosition();
                this.setData(this._page1, this._page1Index);
                this.setData(this._page2, this._page2Index);
            };
        }
        override public function set width(_arg1:Number):void{
            super.width = _arg1;
            this._sizeChange = true;
            if (height > 0){
                this.updateDisplayList();
            };
            if (this.stage){
                this.stage.invalidate();
            };
        }
        override public function set height(_arg1:Number):void{
            super.height = _arg1;
            this._sizeChange = true;
            if (width > 0){
                this.updateDisplayList();
            };
            if (this.stage){
                this.stage.invalidate();
            };
        }
        public function get currentPage():int{
            if (this._cPage == this._page1){
                return (this._page1Index);
            };
            return (this._page2Index);
        }
        private function itemClickHandler(_arg1:MouseEvent):void{
            dispatchEvent(new UIEvent(UIEvent.LISTITEM_CLICK, false, false, _arg1.currentTarget));
        }
        public function get selectedIndex():int{
            return (this._selectedIndex);
        }
        public function set selectedIndex(_arg1:int):void{
            var _local2:int;
            var _local3:Array;
            var _local4:int;
            var _local5:DisplayObjectContainer;
            if (((((!((this.source == null))) && ((this.source.length > _arg1)))) && (!((this._selectedIndex == _arg1))))){
                this._selectedIndex = _arg1;
                _local3 = [this._page1, this._page2];
                _local4 = 0;
                while (_local4 < _local3.length) {
                    _local5 = _local3[_local4];
                    _local2 = 0;
                    while (_local2 < _local5.numChildren) {
                        if ((_local5.getChildAt(_local2) as IListItem).index == _arg1){
                            (_local5.getChildAt(_local2) as IListItem).selected = true;
                        } else {
                            if ((_local5.getChildAt(_local2) as IListItem).selected == true){
                                (_local5.getChildAt(_local2) as IListItem).selected = false;
                            };
                        };
                        _local2++;
                    };
                    _local4++;
                };
                dispatchEvent(new UIEvent(UIEvent.SELECTED_INDEX_CHANGE));
            };
        }
        public function get maxX():Number{
            if (this._direction == Direction.VERTICAL){
                return (0);
            };
            if (this.source == null){
                return (0);
            };
            if (this._cVCount <= 1){
                return (((this.source.length - this._cHCount) * (this._itemWidth + this._cItemHDistance)));
            };
            return ((((this._itemWidth + this._cItemHDistance) * this._cHCount) * (this.pageLength - 1)));
        }
        public function get minX():Number{
            return (0);
        }
        public function get maxY():Number{
            if (this._direction == Direction.HORIZONTAL){
                return (0);
            };
            if (this.source == null){
                return (0);
            };
            if (this._cHCount <= 1){
                return (((this.source.length - this._cVCount) * (this._itemHeight + this._cItemVDistance)));
            };
            return ((((this._itemHeight + this._cItemVDistance) * this._cVCount) * (this.pageLength - 1)));
        }
        public function get minY():Number{
            return (0);
        }
        public function get itemLength():int{
            if (this.source == null){
                return (0);
            };
            return (this.source.length);
        }
        public function get pageLength():int{
            return (Math.ceil((this.itemLength / (this._cHCount * this._cVCount))));
        }
        public function get hCount():int{
            return (this._cHCount);
        }
        public function get vCount():int{
            return (this._cVCount);
        }
        public function getItemX(_arg1:int):Number{
            var _local2:int = (_arg1 / (this._cHCount * this._cVCount));
            var _local3:int = (_arg1 % (this._cHCount * this._cVCount));
            _local3 = (_local3 % this._cHCount);
            var _local4:Number = 0;
            if (this._direction == Direction.VERTICAL){
                _local4 = (_local3 * (this._itemWidth + this._cItemHDistance));
            } else {
                _local4 = (((this._cHCount * (this._itemWidth + this._cItemHDistance)) * _local2) + (_local3 * (this._itemWidth + this._cItemHDistance)));
            };
            return (_local4);
        }
        public function getItemY(_arg1:int):Number{
            var _local2:int = (_arg1 / (this._cHCount * this._cVCount));
            var _local3:int = (_arg1 % (this._cHCount * this._cVCount));
            _local3 = (_local3 / this._cHCount);
            var _local4:Number = 0;
            if (this._direction == Direction.HORIZONTAL){
                _local4 = (_local3 * (this._itemHeight + this._cItemVDistance));
            } else {
                _local4 = (((this._cVCount * (this._itemHeight + this._cItemVDistance)) * _local2) + (_local3 * (this._itemHeight + this._cItemVDistance)));
            };
            return (_local4);
        }
        public function getPageX(_arg1:int):Number{
            return (((this._cHCount * (this._itemWidth + this._cItemHDistance)) * _arg1));
        }
        public function getPageY(_arg1:int):Number{
            return (((this._cVCount * (this._itemHeight + this._cItemVDistance)) * _arg1));
        }
        public function toItemIndex(_arg1:Number, _arg2:Number):int{
            var _local3:int = (_arg1 / (this._cHCount * (this._itemWidth + this._cItemHDistance)));
            var _local4:int = (_arg2 / (this._cVCount * (this._itemHeight + this._cItemVDistance)));
            var _local5:int = Math.max(_local3, _local4);
            var _local6:int = (_arg1 % (this._cHCount * (this._itemWidth + this._cItemHDistance)));
            var _local7:int = (_arg2 % (this._cVCount * (this._itemHeight + this._cItemVDistance)));
            var _local8:int = (_local6 / (this._itemWidth + this._cItemHDistance));
            var _local9:int = (_local7 / (this._itemHeight + this._cItemVDistance));
            return ((((_local5 * (this._cHCount * this._cVCount)) + (_local9 * this._cHCount)) + _local8));
        }
        public function toPageIndex(_arg1:Number, _arg2:Number):int{
            var _local3:int = (_arg1 / (this._cHCount * (this._itemWidth + this._cItemHDistance)));
            var _local4:int = (_arg2 / (this._cVCount * (this._itemHeight + this._cItemVDistance)));
            return (Math.max(_local3, _local4));
        }
        private function onEnterFrame(_arg1:Event):void{
            var _local2:int;
            var _local3:int;
            var _local4:Boolean;
            var _local5:Boolean;
            if (this._rolling){
                _local2 = getTimer();
                _local3 = (_local2 - this._preEnterFrameTime);
                this._preEnterFrameTime = _local2;
                this.cX = (this.cX + (_local3 * this._rollSpeedX));
                this.cY = (this.cY + (_local3 * this._rollSpeedY));
                _local4 = false;
                _local5 = false;
                if (this._rollSpeedX < 0){
                    if (this.cX < this._targetX){
                        _local4 = true;
                    };
                } else {
                    if (this._rollSpeedX > 0){
                        if (this.cX > this._targetX){
                            _local4 = true;
                        };
                    } else {
                        _local4 = true;
                    };
                };
                if (this._rollSpeedY < 0){
                    if (this.cY < this._targetY){
                        _local5 = true;
                    };
                } else {
                    if (this._rollSpeedY > 0){
                        if (this.cY > this._targetY){
                            _local5 = true;
                        };
                    } else {
                        _local5 = true;
                    };
                };
                if (((_local4) && (_local5))){
                    this._rolling = false;
                    this.cX = this._targetX;
                    this.cY = this._targetY;
                    dispatchEvent(new UIEvent(UIEvent.LIST_ROLL_COMP));
                };
            };
        }
        public function rollSpeed(_arg1:Number, _arg2:Number, _arg3:Number):void{
            this._preEnterFrameTime = getTimer();
            this._targetX = _arg1;
            this._targetY = _arg2;
            if (this._targetX < cX){
                this._rollSpeedX = (_arg3 * -1);
            } else {
                if (this._targetX > cX){
                    this._rollSpeedX = _arg3;
                } else {
                    this._rollSpeedX = 0;
                };
            };
            if (this._targetY < cY){
                this._rollSpeedY = (_arg3 * -1);
            } else {
                if (this._targetY > cY){
                    this._rollSpeedY = _arg3;
                } else {
                    this._rollSpeedY = 0;
                };
            };
            this._rolling = true;
        }
        public function rollTime(_arg1:Number, _arg2:Number, _arg3:int):void{
            var _local4:Number = (Math.abs((this.cX - _arg1)) / _arg3);
            var _local5:Number = (Math.abs((this.cY - _arg2)) / _arg3);
            var _local6:Number = Math.max(_local4, _local5);
            this.rollSpeed(_arg1, _arg2, _local6);
        }
        public function stopRoll():void{
            var _local1:int;
            var _local2:Number;
            var _local3:Number;
            if (this._rolling == true){
                _local1 = this.toItemIndex(this.cX, this.cY);
                _local2 = this.getItemX(_local1);
                _local3 = this.getItemY(_local1);
                if (this._rollSpeedX > 0){
                    _local2 = (_local2 + (this._itemWidth + this._cItemHDistance));
                };
                if (this._rollSpeedY > 0){
                    _local3 = (_local3 + (this._itemHeight + this._cItemVDistance));
                };
                this._targetX = _local2;
                this._targetY = _local3;
            };
        }
        public function stopRollToPage():void{
            var _local1:int;
            var _local2:Number;
            var _local3:Number;
            if (this._rolling == true){
                _local1 = this.toPageIndex(this.cX, this.cY);
                _local2 = 0;
                _local3 = 0;
                if (this._direction == Direction.HORIZONTAL){
                    _local2 = this.getPageX(_local1);
                } else {
                    _local3 = this.getPageY(_local1);
                };
                if (this._rollSpeedX > 0){
                    _local2 = (_local2 + ((this._itemWidth + this._cItemHDistance) * this._cHCount));
                };
                if (this._rollSpeedY > 0){
                    _local3 = (_local3 + ((this._itemHeight + this._cItemVDistance) * this._cVCount));
                };
                this._targetX = _local2;
                this._targetY = _local3;
            };
        }
        private function getItemCount(_arg1:Number, _arg2:Number, _arg3:Number):int{
            var _local4:int = (_arg1 / (_arg2 + _arg3));
            while (((_arg2 * (_local4 + 1)) + (_arg3 * _local4)) <= _arg1) {
                _local4++;
            };
            return (_local4);
        }

    }
}